package com.xique.park.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.xique.common.core.constant.Constants;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.PageResult;
import com.xique.park.bean.domain.ParkCar;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.bean.domain.ParkingOrder;
import com.xique.park.bean.dto.ParkInDto;
import com.xique.park.bean.dto.ParkOutDto;
import com.xique.park.bean.request.web.car.ParkCarMultSearchRequest;
import com.xique.park.bean.request.web.parkcar.ParkCarRequest;
import com.xique.park.bean.vo.web.car.ParkCarMultSearchVo;
import com.xique.park.controller.client.web.bean.dto.ParkInOut;
import com.xique.park.service.service.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 多位多车记录查询服务实现
 *
 * @author wld
 * @date 2022/3/15 9:20
 */
@RequiredArgsConstructor
@Service
public class ParkCarMultSearchServiceImpl implements IParkCarMultSearchService {

    private final IParkOutService parkOutService;

    private final IParkCarService parkCarService;

    private final IParkOnService parkOnService;

    private final IParkingOrderService parkingOrderService;

    @Override
    public PageResult selectParkCarMultPageList(ParkCarRequest request) {
        return parkCarService.selectParkCarMultPageList(request);
    }

    @Override
    public ParkCarMultSearchVo selectDetailList(ParkCarMultSearchRequest request) {
        Long id = request.getId();
        Long parkId = SecurityUtils.getParkId();
        ParkCar parkCar = parkCarService.selectParkCarById(id);
        ParkCarMultSearchVo parkCarMultSearchVo = ParkCarMultSearchVo.builder()
                .spaces(parkCar.getSpaces()).seatNo(parkCar.getSeatNo()).endDate(parkCar.getEndDate())
                .userName(parkCar.getUserName()).phone(parkCar.getPhone()).address(parkCar.getAddress())
                .build();

        // 车辆数
        String[] plates = parkCar.getBindPlates().split(",");
        int carCnt = plates.length;
        parkCarMultSearchVo.setCarCnt(carCnt);

        // 计算需要查询的车辆列表
        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = pageNum * pageSize - 1;
        if (startIndex < 0) {
            startIndex = 0;
            endIndex = 9;
        }
        if (startIndex > carCnt) {
            startIndex = carCnt;
        }
        if (endIndex >= carCnt) {
            endIndex = carCnt - 1;
        }

        List<ParkInOut> parkInOutList = new ArrayList<>();
        for (int i = startIndex; i <= endIndex; i++) {
            ParkInOut parkInOut = new ParkInOut();
            // 车牌
            String plate = plates[i];
            parkInOut.setInCarPlate(plate);

            // 获取车辆在场记录
            List<ParkOn> parkOnList = parkOnService.selectParkOnByPlate(parkId, plate);
            if (CollUtil.isNotEmpty(parkOnList)){
                // 在场
                parkInOut.setPresence(Constants.YES);

                ParkOn parkOn = parkOnList.get(0);

                parkInOut.setInTime(parkOn.getInTime());
                parkInOut.setLaneName(parkOn.getLaneName());
                parkInOut.setInType(parkOn.getInType());
                parkInOut.setInStatus(parkOn.getInStatusLabel());

                String parkingSerial = parkOn.getParkingSerial();
                parkInOut.setParkingSerial(parkingSerial);

                // 获取入场信息对应的出场信息
                List<ParkOutDto> parkOutDtoList = parkOutService.selectParkOutByParkingSerial(parkId, parkingSerial);
                if (CollectionUtils.isNotEmpty(parkOutDtoList)) {
                    ParkOutDto parkOutDto = parkOutDtoList.get(0);
                    parkInOut.setOutTime(parkOutDto.getOutTime());
                    parkInOut.setOutLaneName(parkOutDto.getLaneName());
                    parkInOut.setOutType(parkOutDto.getOutType());
                    parkInOut.setOutStatus(parkOutDto.getStatus());
                }

                // 获取订单信息
                ParkingOrder parkingOrderSearch = new ParkingOrder();
                parkingOrderSearch.setParkId(parkId);
                parkingOrderSearch.setParkingSerial(parkingSerial);
                List<ParkingOrder> parkingOrderList = parkingOrderService.selectParkingOrderList(parkingOrderSearch);
                if (CollUtil.isNotEmpty(parkingOrderList)) {
                    parkInOut.setParkingOrderId(parkingOrderList.get(0).getId());
                }
            } else {
                // 没有在场信息
                parkInOut.setPresence(Constants.NO);
                parkInOut.setLaneName("/");
                parkInOut.setOutLaneName("/");
                parkInOut.setInType("/");
                parkInOut.setOutType("/");
            }

            parkInOutList.add(parkInOut);
        }
        parkCarMultSearchVo.setParkCarList(parkInOutList);

        // 在场车辆
        int onSiteCarNum = getOnSiteCarNum(plates, parkId);
        parkCarMultSearchVo.setOnSiteCarNum(onSiteCarNum);

        // 剩余车位数
        int surplusNum = parkCar.getSpaces() - onSiteCarNum;
        if (surplusNum < 0) {
            surplusNum = 0;
        }
        parkCarMultSearchVo.setSurplusNum(surplusNum);

        parkCarMultSearchVo.setPageNum(pageNum);
        parkCarMultSearchVo.setPageSize(pageSize);
        parkCarMultSearchVo.setTotalPages((long) Math.ceil((double) carCnt / pageSize));
        parkCarMultSearchVo.setTotalSize((long) carCnt);

        return parkCarMultSearchVo;
    }

    private int getOnSiteCarNum(String[] plates, Long parkId) {
        List<ParkOn> parkOnList = parkOnService.selectParkOnByPlateList(parkId, Arrays.asList(plates));
        return parkOnList.size();
    }

}
